package com.cash.app.utils

import android.util.Log

object TLog {

    private var logLevel = Log.DEBUG
    private var showLog = true

    // Initialize logging configuration
    fun initTLog(isShow: Boolean = true, level: Int = Log.DEBUG) {
        logLevel = level
        showLog = isShow
    }

    // Log debug message
    fun d(message: String, tag: String? = null, throwable: Throwable? = null) {
        if (showLog && logLevel <= Log.DEBUG) {
            val finalTag = tag ?: getDefaultTag()
            if (throwable == null) {
                Log.d(finalTag, message)
            } else {
                Log.d(finalTag, message, throwable)
            }
        }
    }

    // Log info message
    fun i(message: String, tag: String? = null, throwable: Throwable? = null) {
        if (showLog && logLevel <= Log.INFO) {
            val finalTag = tag ?: getDefaultTag()
            if (throwable == null) {
                Log.i(finalTag, message)
            } else {
                Log.i(finalTag, message, throwable)
            }
        }
    }

    // Log warning message
    fun w(message: String, tag: String? = null, throwable: Throwable? = null) {
        if (showLog && logLevel <= Log.WARN) {
            val finalTag = tag ?: getDefaultTag()
            if (throwable == null) {
                Log.w(finalTag, message)
            } else {
                Log.w(finalTag, message, throwable)
            }
        }
    }

    // Log error message
    fun e(message: String, tag: String? = null, throwable: Throwable? = null) {
        if (showLog && logLevel <= Log.ERROR) {
            val finalTag = tag ?: getDefaultTag()
            if (throwable == null) {
                Log.e(finalTag, message)
            } else {
                Log.e(finalTag, message, throwable)
            }
        }
    }

    // Get default logging tag
    private fun getDefaultTag(): String {
        return "TLogTag"
    }
}